/**

 @Name：layuiAdmin table top filter
 @Author：flc
 @Site：http://www.layui.com/admin/
 @License：LPPL
    
 */
 
layui.define('table',function(exports){
  var $ = layui.$;
  var table = layui.table;
  var props = {
    className : '.table-top-filter' ,
    childName : '.table-filter-alone' ,
    filterName : '.filter-list',
    iconUp : 'layui-icon-up',
    iconDown : 'layui-icon-down',
    valueName : '.filter-value'
  };
  var totalFilter = {

    init: function (tableName){
      var _this = this;


      $('[lay-id='+tableName+'] '+props.className).each(function(i,item) {
        $(item).append('<div class="filter-list" style="display:none"></div><i class="layui-icon layui-icon-up"></i>');
        $(item).children('i').click(function(){
          _this.styleToggle(this);
          table.resize(tableName);
        });
      });

      setTimeout(function(){
        table.resize(tableName);
      },0);

    },

    styleToggle: function(dom){
      var $parent = $(dom).parent();
      var $childName = $parent.find(props.childName);
      var $filterName = $parent.find(props.filterName);
      $childName.toggle();
      $filterName.toggle();
      if($(dom).hasClass(props.iconUp)){
        this.childList( $childName , $filterName);
        $(dom).removeClass(props.iconUp);
        $(dom).addClass(props.iconDown);
      }else{
        $(dom).removeClass(props.iconDown);
        $(dom).addClass(props.iconUp);
      }
    },

    childList : function ( list , box ) {
      box.html('<label>筛选条件：</label>')
      list.each(function(i,item){
        var text = $(item).find('label').text();
        var valName = $(item).find(props.valueName);
        var val = [];
        //如果是select框
        if($(valName).is("select")){
            $(valName).find("option").each(function(j,items){
              if(items.selected){
                if(items.value){
                  val.push(items.text);
                }else{
                  val.push('未选择');
                }
              }
          })
        }else{
          if(valName.length>1){
            valName.each(function(j,items){
              val.push($(items).val() || '未选择');
            })
          }else{
            val.push(valName.val() || '未选择');
          }
        }
        box.append('<div class="filterLi">' + text+ ':' +val.join(',')+ '</div>')
      })
    }
  };

  
  //对外暴露的接口
  exports('totalFilter', totalFilter);
});